package com.subat.demo.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subat.demo.common.Result;
import com.subat.demo.entity.Course;
import com.subat.demo.service.ICourseService;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.subat.demo.service.IMajorCourseService;
import com.subat.demo.entity.MajorCourse;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author subat
 * @since 2022-06-02
 */
@RestController
@RequestMapping("/major-course")
public class MajorCourseController {

    @Resource
    private IMajorCourseService majorCourseService;

    @Resource
    private ICourseService courseService;

//    新增或者更新信息
    @PostMapping
    public Result save(@RequestBody List<MajorCourse> majorCourses) {
        majorCourseService.saveAll(majorCourses);
        return Result.success();
    }

//    删除信息
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(majorCourseService.removeById(id));
    }

//    批量删除信息
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(majorCourseService.removeByIds(ids));
    }

//    查询所有数据
    @GetMapping
    public Result findAll() {
        return Result.success(majorCourseService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(majorCourseService.getById(id));
    }

    @GetMapping("/major/{majorId}")
    public Result findByMajor(@PathVariable Integer majorId) {
        return Result.success(majorCourseService.list(new QueryWrapper<MajorCourse>().eq("major_id", majorId)));
    }

//    查询专业课程表
    @GetMapping("/courseTable/{majorId}")
    public Result findCourseTable(@PathVariable Integer majorId) {
        List<MajorCourse> majorCourses = majorCourseService.list(new QueryWrapper<MajorCourse>().eq("major_id", majorId));
        List<JSONObject> list = CollUtil.newArrayList();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        JSONObject jsonObject3 = new JSONObject();
        JSONObject jsonObject4 = new JSONObject();
        JSONObject jsonObject5 = new JSONObject();
        jsonObject1.set("section", JSONUtil.parseObj("{\"num\": \"第一大节\", \"time\": \"08:30-10:00\"}"));
        jsonObject2.set("section", JSONUtil.parseObj("{\"num\": \"第二大节\", \"time\": \"10:20-11:50\"}"));
        jsonObject3.set("section", JSONUtil.parseObj("{\"num\": \"第三大节\", \"time\": \"13:30-15:00\"}"));
        jsonObject4.set("section", JSONUtil.parseObj("{\"num\": \"第四大节\", \"time\": \"15:20-16:50\"}"));
        jsonObject5.set("section", JSONUtil.parseObj("{\"num\": \"第五大节\", \"time\": \"18:30-20:00\"}"));
        list.add(jsonObject1);
        list.add(jsonObject2);
        list.add(jsonObject3);
        list.add(jsonObject4);
        list.add(jsonObject5);
        majorCourses.forEach(mc -> {
            Integer courseId = mc.getCourseId();
            Course course = courseService.getById(courseId);
            String weekDay = mc.getWeekDay();
            String section = mc.getSection();
            switch (section) {
                case "第一大节":
                    setJson(weekDay, course, jsonObject1);
                    break;
                case "第二大节":
                    setJson(weekDay, course, jsonObject2);
                    break;
                case "第三大节":
                    setJson(weekDay, course, jsonObject3);
                    break;
                case "第四大节":
                    setJson(weekDay, course, jsonObject4);
                    break;
                case "第五大节":
                    setJson(weekDay, course, jsonObject5);
                    break;
            }
        });
        return Result.success(list);
    }

    private JSONObject setJson(String weekDay, Course course, JSONObject jsonObject) {
        if ("周一".equals(weekDay)){
            jsonObject.set("mon", course);
        } else if ("周二".equals(weekDay)) {
            jsonObject.set("tue", course);
        } else if ("周三".equals(weekDay)) {
            jsonObject.set("wes", course);
        } else if ("周四".equals(weekDay)) {
            jsonObject.set("thu", course);
        } else if ("周五".equals(weekDay)) {
            jsonObject.set("fri", course);
        } else if ("周六".equals(weekDay)) {
            jsonObject.set("sat", course);
        } else if ("周日".equals(weekDay)) {
            jsonObject.set("sun", course);
        }
        return jsonObject;
    }

    //    分页查询和模糊查询（Mybatis-plus版）
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<MajorCourse> queryWrapper = new QueryWrapper<>();
        return Result.success(majorCourseService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}
